<template>
  <div class="list">
    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">
        <span style="margin-left: 20px; margin-right: 6px;">限定开始日期:</span>
        <iDatePicker :showTime="false" style="margin-left: 10px;" placeholder="不选则不限制" v-model="filter.start_day"></iDatePicker>
        
        <div class="tab-list">
           <div class="item" @click="ad_type = item.key" :class="{actived: ad_type === item.key}"
             v-for="(item, index) in adTypeList">
             {{item.text}}
           </div>
         </div>
         
        <div class="tab-list">
           <div class="item" @click="type_key = item.key" :class="{actived: type_key === item.key}"
             v-for="(item, index) in typeList">
             {{item.text}}
           </div>
         </div>
      </div>
    </div>
    <a-table class="table" :rowKey="row => row.id" :dataSource="list" :pagination="false" :size="size">

      <a-table-column :title="title[0]" width="100px">
        <template slot-scope="row">
			<span>{{row.month}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[1]" width="100px">
        <template slot-scope="row">
			<span>{{row.add_user_num}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[2]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_1}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[3]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_2}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[4]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_3}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[5]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_4}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[6]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_5}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[7]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_6}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[8]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_7}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[9]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_8}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[10]" width="100px">
        <template slot-scope="row">
			<span>{{row.value_9}}</span>
        </template>
      </a-table-column>
      <a-table-column :title="title[11]" width="100px">
        <template slot-scope="row">
      <span>{{row.value_10}}</span>
        </template>
      </a-table-column>
    </a-table>						

  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'

  import NumberGroupDisplay from './components/NumberGroupDisplay'

  import IDatePicker from "@/components/IDatePicker/IDatePicker.vue"

  const resourceType = 'stats/wx-user-month-pay'
  const resourceTypeText = '广告用户营收/利润'

  export default {
    components: {
      ISwitch,
      NumberGroupDisplay,
      IDatePicker
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isEdit: false,
        editUuid: '',
        resourceTypeText,
        exportOptions: {
          status: 'all',
          startTime: '',
          endTime: '',
          type: 'normal'
        },
        type_key: 'month_pay_data',
        typeList: [
          {
            text: '营收',
            key: 'month_pay_data'
          },
          {
            text: '利润',
            key: 'month_profit_data'
          }
        ],
        ad_type: '1',
        adTypeList: [
          {
            text: '微信广告',
            key: '1'
          },
          {
            text: '抖音广告',
            key: '2'
          }
        ],        
        title: [],
        list: [],
        page: 1,
        per_page: 30,
        total: 0,
        key: '',
        keyType: 'phone',
        category_id: undefined,
        sortBy: undefined,
        filter: {
          start_day: '',
          max_day: ''
        }
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      category_id() {
        this.refreshData()
      },
      type_key() {
        this.refreshData()
      },
      ad_type() {
        this.refreshData()
      },      
      sortBy() {
        this.refreshData()
      },
      'filter.start_day'() {
        this.refreshData()
      },
      'filter.max_day'() {
        this.refreshData()
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          current: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e
            this.fetchData()
          }
        }
      }
    },
    filters: {},
    created() {
      this.fetchData()
      // this.getCategoryList()
    },
    methods: {
      setTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'set_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      unsetTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要取消置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'unset_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.uuid}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      updateItem(row, data) {
        return this.$http.patch(`/${resourceType}/${row.uuid}`, {
          type: 'update',
          attributes: {
            ...data
          }
        })
      },
      recommendStatusChange(e, row) {
        this.updateItem(row, {
          is_recommend: e
        }).then(res => {
          this.fetchData()
        })
      },
      listedStatusChange(e, row) {
        this.updateItem(row, {
          status: e
        }).then(res => {
          this.fetchData()
        })
      },
      handleEditListWeight(row) {
        this.$globalForm.show({
          title: '编辑排序',
          items: [{
            type: 'number',
            key: 'list_weight',
            title: '排序',
            default: row.list_weight,
            required: true
          }, ],
          success: (data) => {
            this.updateItem(row, {
              ...data
            }).then(res => {
              this.$message.success('保存成功');
              this.$globalForm.hide()
              this.fetchData()
            })
          }
        })
      },
      handleCreate() {
        this.editUuid = ""
        this.isEdit = true
      },
      handleEdit(row) {
        this.editUuid = row.uuid
        this.isEdit = true
      },
      refreshData() {
        this.page = 1
        // this.key = ''
        this.fetchData()
      },
      onSearch(e) {
        this.fetchData()
        // clearTimeout(this.timeout)
        // this.timeout = setTimeout(() => {
        //   this.fetchData()
        // }, 300)
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            type_key: this.type_key,
            type: this.ad_type,
            start_day: this.filter.start_day,
          }
        }).then(res => {
          this.list = res.data.list
          this.title = res.data.title
        }).finally(() => {
          this.loading = false
        })
      },
      getCategoryList() {
        this.$api.emit('category.list', 'article').then(res => {
          this.category = res.data.list
        })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .top-btn {
    background: #1890FF;
    color: white;
    cursor: pointer;
    font-size: 80%;
    border-radius: 4px;
    padding: 2px 6px;

    &.unset {
      border: 1px solid #D8D5D5;
      background: #D8D5D5;
      color: gray;
    }
  }

  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;

      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }

      .gender-man,
      .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }

      .gender-man {
        color: #409EFF;
      }

      .gender-woman {
        color: #F56C6C;
      }
    }
  }
  .sort-keys {
    display: flex;
    align-items: center;
  
    .tab-list {
      display: flex;
      margin-top: 6px;
      border-bottom: 1px solid #e1e1e1;
      margin-left: 40px;
  
      .item {
        margin-right: 10px;
        margin-left: 10px;
        padding: 0px 4px 4px 4px;
        cursor: pointer;
        font-weight: 500;
  
        &.actived {
          color: #1890FF;
          border-bottom: 3px solid #1890FF;
          font-weight: bold;
        }
  
        &:hover {
          color: #1890FF;
        }
      }
    }
  
  }
</style>
